<?php declare(strict_types=1);
/**
 * @author      xianganyall <xianganyall@gmail.com>
 * @copyright   2023-2025 owner
 **/

namespace Srv\Apps\Apis\Common\DefCtl;

use Srv\Libs\Common\CommTime;
use Srv\Libs\Frame\ControllerAbstract;
use Srv\Libs\Storage\DataMysql;

final class DefMobileCodeCtl extends ControllerAbstract
{
    private ?DataMysql $DbAuto              = null;     // DB-Write
    private int $cacheTime                  = 600;      // 缓存时长[秒][0-长期][<0-不缓存]
    private int $cacheLastTime              = 0;        // 缓存的数据最后时间
    private array $cacheDataList            = [];       // 缓存的数据列表
    private array $cacheIdxList             = [];       // 缓存的索引列表
    public const IDX_ID                     = 'id';
    public const IDX_CODE                   = 'code';
    public const IDX_NAME                   = 'name';
    private string $tableName               = 'def_mobile_code';
    private string $fieldId                 = 'mbct_id';
    private string $fieldCode               = 'mbct_code';
    private string $fieldName               = 'mbct_name';
    private string $fieldStatus             = 'mbct_status';

    /**
     * @return static
     * tag
     */
    public static function tag():self
    {
        $thisObject                         = self::tagGet();
        if($thisObject instanceof self){
            $thisObject->DbAuto             = self::getDbAuto();
        }
        $thisObject->loadData(true);
        return $thisObject;
    }

    /**
     * @param bool $isForce
     * @return bool
     * loadData
     */
    public function loadData(bool $isForce):bool
    {
        $currTime               = 0;
        if(!$isForce && $this->cacheLastTime > 0){
            if($this->cacheTime === 0) return true;
            $currTime           = CommTime::getTimeStamp();
            if(($currTime - $this->cacheLastTime) < $this->cacheTime) return true;
        }
        if($currTime < 1) $currTime = CommTime::getTimeStamp(0);
        $this->cacheLastTime    = $currTime;
        $this->cacheDataList    = [];
        $this->cacheIdxList     = [];
        $isFail                 = false;
        $listData               = $this->DbAuto->getData('SELECT `'.$this->fieldId.'` AS _def_id, `'.$this->fieldCode.'` AS _def_code, `'.$this->fieldName.'` AS _def_name FROM '.$this->DbAuto->getTableNameAll($this->tableName).' WHERE `'.$this->fieldStatus.'`=1 ORDER BY `'.$this->fieldName.'` ASC, `'.$this->fieldId.'` ASC', $isFail);
        if($isFail) return false;
        if(count($listData) > 0) foreach($listData as $listItem){
            $idItem             = intval($listItem['_def_id']??0);
            $codeItem           = intval($listItem['_def_code']??0);
            $nameItem           = $listItem['_def_name']??'';
            if($idItem < 1 || $codeItem < 1 || strlen($nameItem) < 1) continue;
            $this->cacheDataList[$idItem]   = [self::IDX_ID => $idItem, self::IDX_CODE => $codeItem, self::IDX_NAME => $nameItem];
            $this->cacheIdxList[]           = $codeItem;
        }
        return true;
    }

    /**
     * @return array
     * getDataList
     */
    public function getDataList():array
    {
        return array_values($this->cacheDataList);
    }

    /**
     * @param int $codeInt
     * @return bool
     * isAllowCode
     */
    public function isAllowCode(int $codeInt):bool
    {
        if($codeInt < 1) return false;
        return in_array($codeInt, $this->cacheIdxList, true);
    }
}